//define global vars
var loogatPrefs = new gadgets.Prefs(), startUpData = '';
// The function to perform AJAX request for word translation
var requestPerformer = function(url, params) {
	gadgets.io.makeRequest(url, response, params);
};

// Callback function to process the response
var response = function(obj) {
	document.getElementById(loogatPrefs.getModuleId()+'content').innerHTML = obj.text;
};

// function to perform translation
var translate = function(word) {
	if (!word) {
		word = document.getElementById('loogatWord').value;
	}
	gadgets.io.makeRequest(loogatPrefs.getString('url') + word + '&script=latin', response);
};

// function to deep into word translation options
var getWord = function(word) {
	translate(word);
};

// displays/hides copyright info
var toggleHelper = function(elem) {
	var item = document.getElementById(loogatPrefs.getModuleId()+'copyrightContainer');
	if (item.style.display == 'none') {
		item.style.display = 'block';
	} else {
		item.style.display = 'none';
	}
	gadgets.window.adjustHeight();
};
// loads startup data
var loogatLoadStartUpData = function() {
	var forumUrl = loogatPrefs.getString('forumUrl'), params = {};
	params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
	gadgets.io.makeRequest(forumUrl, function(obj) {
		if (obj.data) {
			var rssItems = obj.data.getElementsByTagName("item"), amount = 4;
			startUpData += '<span style="font-weight:bold; font-size: 11pt;">'+loogatPrefs.getMsg('recentPosts')+'</span>:';
			for ( var i = 0; rssItems && i < rssItems.length; i++) {
				startUpData += '<br> - <a href="';
				var href = '', title = '', category = '';
				for ( var ch = 0; rssItems[i] && rssItems[i].childNodes
						&& ch < rssItems[i].childNodes.length; ch++) {
					var item = rssItems[i].childNodes[ch];
					if (item && item.nodeName == 'title' && item.firstChild) {
						title = item.firstChild.nodeValue;
					}
					if (item && item.nodeName == 'link' && item.firstChild) {
						href = item.firstChild.nodeValue;
					}
					if (item && item.nodeName == 'category' && item.firstChild) {
						category = item.firstChild.nodeValue;
					}
				}
				startUpData += href+'" title="'+category+' : '+title+'" style="font-size: 10pt;" target="_blank">'+category+' :: '+title+ "</a>";
				if (i >= amount) {
					document.getElementById(loogatPrefs.getModuleId()+'content').innerHTML = startUpData;
					break;
				}
			}
		}
	}, params);
};
var loogatClearTranslation = function() {
	document.getElementById(loogatPrefs.getModuleId()+'content').innerHTML = '';
	document.getElementById(loogatPrefs.getModuleId()+'content').innerHTML = startUpData;
	document.getElementById('loogatWord').value = '';
};
// application starter
var loogatStarter = function() {
	// change noCacheId
	loogatPrefs.set('noCacheId', new Date().getTime());
	// load startup data
	loogatLoadStartUpData();
};
// assign starter
gadgets.util.registerOnLoadHandler(loogatStarter);